// JavaScript Document
$(function(){
	
	//0、更改滚动条样式
	 $(".musicList").mCustomScrollbar(); 
	 //播放模式
	 var $audio=$("audio");
	 var player=new Player($audio);
     var progress;
	 var voiceProgress;
	 var lyric;
		
		
		
	/*1、获取歌曲*/
	musicPlayList()
	/* var $contentList=$(".musicList ul");*/
	
	function musicPlayList(){
		$.ajax({
			url:"source/musiclist.json",
			dataType:"json",
			success:function(data){
				player.contentList= data;
				
				var $contentList=$(".musicList ul");
				$.each(data,function(index,ele){
					 var $item =creatItem(index,ele);
					 $contentList.append($item);
					})
					//初始化歌曲
					initMusicInfo(data[0]);
					//初始化歌词
					initLoadMusicInfo(data[0])
			},
			error:function(e){
				console.log(e)		
			}
			})
			
			//添加一条音乐
			function creatItem(index,music){
				var $item=$("<li class=\"listall\">\n"+
                      "<div class=\"music_check\"><span></span></div>\n"+
                      "<div class=\"music_num \">"+(index+1)+"</div>\n"+
                      "<div class=\"music_name\">"+music.name+"<div class=\"music_log\">\n"+
                               " <ul>\n"+
                               " <span><li><a href=\"#\" title=\"播放\" class=\"play\" id=\"play1\"></a></li></span>\n"+
                               " <span><li><a href=\"#\" title=\"添加\"></a></li></span>\n"+
                              " <span><li><a href=\"#\" title=\"下载\"></a></li></span>\n"+
                              "<span><li><a href=\"#\" title=\"分享\"></a></li></span>\n"+
                               " </ul>\n"+
                          " </div>\n"+  
                     " </div>\n"+
                     " <div class=\"music_singer\">"+music.singer+"</div>\n"+
                      "<div  class=\"music_times\" >\n"+
                      "<span>"+music.time+"</span>\n"+
                      "<a href=\"#\" title=\"删除\"  class=\"listall_del\"></a>\n"+
                      "</div>\n"+
                   "</li>")
				$item.get(0).index=index;
				$item.get(0).music=music;
				return $item;
				}
		};
		//进度条
		progressInit()
		function progressInit(){
			//动态播放时间进度条
	   var $progress=$(".song_progress");
	   var $progressIn=$(".song_progress_in");
	   var $progressTar=$(".song_progress_tar");
	   progress=new Progress($progress,$progressIn,$progressTar);
	   progress.progressClick(function(value){
		   player.musicSeekTo(value);
		   console.log(value)
		   });
	   progress.progressMove(function(value){
		   player.musicSeekTo(value);
		   });
		//声音控制条
		var $voiceProgress=$(".voice_progress");
	   var $voiceProgressIn=$(".voice_progress_in");
	   var $voiceProgressTar=$(".voice_progress_tar");
	    voiceProgress=new Progress($voiceProgress,$voiceProgressIn,$voiceProgressTar);
	   voiceProgress.progressClick(function(value){
		  player.musicVioceSeekTo(value);
		   });
	  voiceProgress.progressMove(function(value){
		   player.musicVioceSeekTo(value)
		   });
			}
	
    //播放图标的淡入淡出
	$(".musicList").delegate(".listall","mouseenter",function(){
  $(this).find(".music_log").stop().show();
	  $(this).find(".music_times a").stop().show();
	   $(this).find(".music_times span").stop().fadeOut();
		}) 
		$(".musicList").delegate(".listall","mouseleave",function(){
		   $(this).find(".music_log").stop().fadeOut();
		  $(this).find(".music_times a").stop().fadeOut();
		  $(this).find(".music_times span").stop().fadeIn();
		
		
		} )
		
  //初始化歌曲
  function initMusicInfo(music){
	  //获取对应yuans
	var $songName=$(".song_name a");
	var $songSinger=$(".song_singer a");
	var $songAlbum=$(".song_album a");
	var $songBg=$(".song_info img");
	var $maskImg=$(".mask_img");
	var $songPname=$(".song_playinfo_name");
	var $songPtime=$(".song_playinfo_time");
	var $cur=$(".song_content");
	
	//给对应元素赋值
   $maskImg.css("background","url('"+music.cover+"')");
   $songBg.attr("src",music.cover);
   $songName.text(music.name)
	 $songSinger.text(music.singer)
	$songAlbum.text(music.album);
	$songPname.text(music.name+" - "+music.singer);
	$songPtime.text("00:00"+"/"+music.time);
  
  }
  //初始化歌词
  function initLoadMusicInfo (music){
	  
	  lyric = new Lyric(music.link_lrc);
	  var $lyricContent=$(".song_content");
	  $lyricContent.html(" ");
	  lyric.downLyric(function(){
		  //创建歌词列表
		  $.each(lyric.lyrics,function(index,ele){
				  var $item=$("<li >"+ele+"</li>");
				  $lyricContent.append($item);
				  
		  
		  
		  
		  });
		  });
	  
	  
	  }
	
	//监听事件
	spy();	
	function spy(){
		//监听删除按钮
		$(".musicList").delegate(".listall_del","click",function(){
			var $item = $(this).parents(".listall");
			$item.remove();
		  /*$item.css("display","none");*/
		  player.changeMusic($item.get(0).index);
		 //重新排序
		$(".listall").each(function(index,ele){
			ele.index = index;
			$(ele).find(".music_num").text(index+1);
			console.log(ele)
			
			});
			//判断删除是否是当前播放歌曲
			if($item.get(0).index==player.currentIndex){
				//播放下一首
				$(".play_next").trigger("click");
				
				}
		})
		
			
		//复选框
	$(".musicList").delegate(".music_check","click",function(){
		$(this).toggleClass("checkimg");
		})
		//监控播放菜单
		var $playing =$(".playing");
		
		
		//子菜单播放按钮
	$(".musicList").delegate("#play1","click",function(){
		
		var $item=$(this).parents(".listall");
		
	/*	console.log($item.get(0).index);
		console.log($item.get(0).music);*/
		//播放浪条
		$item.find(".music_num").toggleClass("music_num2");
		$item.siblings().find(".music_num").removeClass("music_num2");
		
		
		if($(this).attr("class")=="play"){
			//非播放状态，转化为播放状态
			$(this).attr("class","play2");
			//播放状态的排他性
			$item.siblings().find("#play1").attr("class","play");
			//下部也切换成播放状态
			$("#playing").attr("class","playing_change");
			//文字高亮
			$item.find("div").css("color","#fff");
			//文字高亮排他性
			$item.siblings().find("div").css("color","rgba(255,255,255,0.5)");
		
			}else{
			
				$(this).attr("class","play");
				
				$("#playing").attr("class","playing");
				//文字不高亮
				$item.find("div").css("color","rgba(255,255,255,0.5)");
				//播放浪条消失
				$item.find(".music_num").removeClass("music_num2");
				}
			
			//播放歌曲
		player.playMusic($item.get(0).index,$item.get(0).music);
		//切换歌曲xinxi
		initMusicInfo($item.get(0).music);
	    //切换歌词信息
		initLoadMusicInfo($item.get(0).music);
		 //播放时间同步
		 player.getMusicTimeUpDate(function(currentTime,duration,timestr){
			  $(".song_playinfo_time").text(timestr);
			 var value=currentTime/duration*100;
			 //进度条同步
			  progress.progressSynchro(value);
			 //歌词同步
			 var index=lyric.lyricCurrentTime(currentTime);
			 $(".song_contentBox .song_content li").eq(index).addClass("cur");
			 $(" .song_contentBox .song_content li").eq(index).siblings().removeClass("cur");
			 if(index<=0)return
			 
			 $(".song_content").css({
				 marginTop:-index*30
				 
				 
				 })
				
			 
			 });
		
	
		
		
		
		})
		//监控声音图标
		$(".voice_log").click(function(){
	         $(".voice_log").toggleClass("voice_log2")
		      if($(this).attr("class").indexOf("voice_log2")!=-1){
				 //关闭声音
				 player.musicVioceSeekTo(0); 
			}else{
				//开启声音
				player.musicVioceSeekTo(1);
				}
			})
		

	$(".contentIn_nav>span").click(function(){
		alert("asdas")
		/*$(this).find("i").css("background-position",'-162px -202px')*/
	
	})
	//播放
	$("#playing").click(function(){
		if($(this).attr("class")=="playing"){
			$(this).attr("class","playing_change");

			}else{
				$(this).attr("class","playing");
				}
	})
	//下部播放按钮
	$("#playing").click(function(){
		if(player.currentIndex==-1){
			$(".listall").eq(0).find("#play1").trigger("click");
			}else{
			$(".listall").eq(player.currentIndex).find("#play1").trigger("click");	
				
				}
		
		})
	//上一首播放
	$(".play_last").click(function(){
	
		$(".listall").eq(player.lastIndex()).find("#play1").trigger("click"); 
		/*console.log(lastIndex())*/
		
		})

	//下一首播放
	$(".play_next").click(function(){
		
		$(".listall").eq(player.nextIndex()).find("#play1").trigger("click");
		/*console.log(nextIndex())*/
		
		})
	
	//循环播放
	$("#play_again1").click(function(){
		if($(this).attr("class")=="play_again"){
			$(this).attr("class","play_again_change");
			
			
			}else{
				
				$(this).attr("class","play_again");
				
				}
	})
	//喜爱
	$("#play_love1").click(function(){
		if($(this).attr("class")=="play_love"){
			$(this).attr("class","play_love_change");
			
			
			}else{
				
				$(this).attr("class","play_love");
				
				}
	})
	//评论
	$("#play_comment1").click(function(){
		
		if($(this).attr("class")=="play_comment"){
			$(this).attr("class","play_comment_change");
			
			
			}else{
				
				$(this).attr("class","play_comment");
				
				}
	})
	//声道
	$("#play_voice1").click(function(){
		if($(this).attr("class")=="play_voice"){
			$(this).attr("class","play_voice_change");
			
			
			}else{
				
				$(this).attr("class","play_voice");
				
				}
	})
	
		
		
		}



 
	
})